import 'package:flutter/material.dart';
import 'package:liquid_glass_renderer/liquid_glass_renderer.dart';

/// LaraPressBox - 一个功能完整的布局容器组件
/// 提供圆角、背景色、阴影、背景图片、内边距、外边距等功能
class LaraPressBox extends StatelessWidget {
  /// 子组件
  final Widget child;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 外边距
  final EdgeInsetsGeometry? margin;

  /// 背景色
  final Color? backgroundColor;

  /// 背景图片
  final String? backgroundImage;

  /// 背景图片的填充方式
  final BoxFit backgroundImageFit;

  /// 圆角半径
  final BorderRadius? borderRadius;

  /// 边框
  final Border? border;

  /// 阴影
  final List<BoxShadow>? boxShadow;

  /// 渐变背景
  final Gradient? gradient;

  /// 宽度
  final double? width;

  /// 高度
  final double? height;

  /// 对齐方式
  final AlignmentGeometry? alignment;

  /// 是否裁剪子组件
  final bool clipBehavior;

  /// 点击事件
  final VoidCallback? onTap;

  /// 长按事件
  final VoidCallback? onLongPress;

  const LaraPressBox({
    super.key,
    required this.child,
    this.padding,
    this.margin,
    this.backgroundColor,
    this.backgroundImage,
    this.backgroundImageFit = BoxFit.cover,
    this.borderRadius,
    this.border,
    this.boxShadow,
    this.gradient,
    this.width,
    this.height,
    this.alignment,
    this.clipBehavior = true,
    this.onTap,
    this.onLongPress,
  });

  @override
  Widget build(BuildContext context) {
    // 创建装饰对象
    final decoration = BoxDecoration(
      color: backgroundColor,
      borderRadius: borderRadius,
      border: border,
      boxShadow: boxShadow,
      gradient: gradient,
      image: backgroundImage != null
          ? DecorationImage(
              image: AssetImage(backgroundImage!),
              fit: backgroundImageFit,
            )
          : null,
    );

    // 创建内容容器
    Widget contentContainer = Container(
      width: width,
      height: height,
      padding: padding,
      alignment: alignment,
      child: child,
    );

    // 如果需要裁剪
    if (clipBehavior && borderRadius != null) {
      contentContainer = ClipRRect(
        borderRadius: borderRadius!,
        child: contentContainer,
      );
    }

    Widget container;

    // 如果需要点击效果
    if (onTap != null || onLongPress != null) {
      container = Material(
        color: Colors.transparent,
        child: Ink(
          decoration: decoration,
          child: InkWell(
            onTap: onTap,
            onLongPress: onLongPress,
            borderRadius: borderRadius,
            child: contentContainer,
          ),
        ),
      );
    } else {
      // 普通容器
      container = Container(
        width: width,
        height: height,
        padding: padding,
        alignment: alignment,
        decoration: decoration,
        child: child,
      );

      // 如果需要裁剪
      if (clipBehavior && borderRadius != null) {
        container = ClipRRect(borderRadius: borderRadius!, child: container);
      }
    }

    // 添加外边距
    if (margin != null) {
      container = Container(margin: margin, child: container);
    }

    return container;
  }
}

/// LaraPressBox 的便捷构造函数
extension LaraPressBoxConvenience on LaraPressBox {
  /// 创建卡片样式的容器
  static LaraPressBox card({
    Key? key,
    required Widget child,
    EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? margin,
    Color? backgroundColor,
    double borderRadius = 8.0,
    List<BoxShadow>? boxShadow,
    VoidCallback? onTap,
  }) {
    return LaraPressBox(
      key: key,
      padding:
          padding ??
          const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
      margin: margin,
      backgroundColor: backgroundColor ?? Colors.white,
      borderRadius: BorderRadius.circular(borderRadius),
      boxShadow:
          boxShadow ??
          [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.1),
              blurRadius: 4.0,
              offset: const Offset(0, 2),
            ),
          ],
      onTap: onTap,
      child: child,
    );
  }

  /// 创建带背景图片的容器
  static LaraPressBox imageBackground({
    Key? key,
    required Widget child,
    required String backgroundImage,
    EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? margin,
    BoxFit backgroundImageFit = BoxFit.cover,
    double borderRadius = 8.0,
    VoidCallback? onTap,
  }) {
    return LaraPressBox(
      key: key,
      padding: padding,
      margin: margin,
      backgroundImage: backgroundImage,
      backgroundImageFit: backgroundImageFit,
      borderRadius: BorderRadius.circular(borderRadius),
      onTap: onTap,
      child: child,
    );
  }

  /// 创建渐变背景的容器
  static LaraPressBox gradientBackground({
    Key? key,
    required Widget child,
    required Gradient gradient,
    EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? margin,
    double borderRadius = 8.0,
    VoidCallback? onTap,
  }) {
    return LaraPressBox(
      key: key,
      padding: padding,
      margin: margin,
      gradient: gradient,
      borderRadius: BorderRadius.circular(borderRadius),
      onTap: onTap,
      child: child,
    );
  }
}

// 为方便统一管理创建一个什么也没有的盒子
class LaraPressEmptyBox extends StatelessWidget {
  final Widget child;
  const LaraPressEmptyBox({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Material(child: child);
  }
}

class LaraPressGlassBox extends StatelessWidget {
  final Widget child;

  const LaraPressGlassBox({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return LiquidGlass(
      settings: LiquidGlassSettings(
        blur: 2,
        glassColor: Colors.grey.withValues(alpha: .1),
        thickness: 1,
      ),
      shape: LiquidRoundedSuperellipse(borderRadius: Radius.circular(0)),
      child: child,
    );
  }
}
